package systemFile;

import GUIv2.MainWindow;
import general.GlobalProperties;

import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

import GUI.FileAttributesProfilesGUI;
import GUI.GlobalPropertiesGUI;
import GUI.ProfilesEditGUI;
import GUI.ProfilesVersioningGUI;

public class SysTray {
    private static Logger log = Logger.getLogger(SysTray.class);

    public static Image getImage() throws HeadlessException {
	InputStream inputStream = SysTray.class
		.getResourceAsStream("/resources/icons/database.png");
	Image img = null;
	File toWrite = new File(GlobalProperties.settingsDirectory,
		"\\database.png");
	if (!toWrite.exists())
	    writeToFile(inputStream, toWrite);

	Icon defaultIcon = new ImageIcon(toWrite.getAbsolutePath());
	img = new BufferedImage(defaultIcon.getIconWidth(),
		defaultIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
	defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

	return img;
    }

    private static void writeToFile(InputStream is, File file) {
	try {
	    DataOutputStream out = new DataOutputStream(
		    new BufferedOutputStream(new FileOutputStream(file)));
	    int c;
	    while ((c = is.read()) != -1) {
		out.writeByte(c);
	    }
	    is.close();
	    out.close();
	} catch (IOException e) {
	    System.err.println("Error Writing/Reading Streams.");
	}
    }

    public static Image getRefreshImage() throws HeadlessException {
	InputStream inputStream = SysTray.class
		.getResourceAsStream("/resources/icons/database_refresh.png");
	Image img = null;
	File toWrite = new File(GlobalProperties.settingsDirectory,
		"\\database.png");
	if (!toWrite.exists())
	    writeToFile(inputStream, toWrite);

	Icon defaultIcon = new ImageIcon(toWrite.getAbsolutePath());
	img = new BufferedImage(defaultIcon.getIconWidth(),
		defaultIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
	defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

	return img;
    }

    public static Image getErrorImage() throws HeadlessException {
	InputStream inputStream = SysTray.class
		.getResourceAsStream("/resources/icons/database_error.png");
	Image img = null;
	File toWrite = new File(GlobalProperties.settingsDirectory,
		"\\database_error.png");
	if (!toWrite.exists())
	    writeToFile(inputStream, toWrite);
	Icon defaultIcon = new ImageIcon(toWrite.getAbsolutePath());
	img = new BufferedImage(defaultIcon.getIconWidth(),
		defaultIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
	defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

	return img;
    }

    public static PopupMenu createPopupMenu() throws HeadlessException {
	PopupMenu menu = new PopupMenu();

	MenuItem MAIN = new MenuItem("Preferences...");
        MAIN.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        new MainWindow().setVisible(true);
                    }
                });
            }
        });
        menu.add(MAIN);
	
	/*
	 * Exit
	 */
	MenuItem exit = new MenuItem("Exit");
	exit.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		System.exit(0);
	    }
	});
	menu.add(exit);

        


	return menu;
    }
}